package libaryinterface.user;

import libaryinterface.interfaceutils.MyDialogDemo;
import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-15
 * Time: 19:11
 * author:UpdateInterface
 */
//修改用户界面
public class UserIsUpdateInterface extends JFrame {
    public Long id;
    public UserIsUpdateInterface(Object[][] rowdata, int index) {

        //设置窗口
        super("修改界面");
        //1.把图片添加到标签里（把标签的大小设为和图片大小相同），把标签放在分层面板的最底层；
        ImageIcon bg = new ImageIcon("src//picture//ice.jpg");
        JLabel label = new JLabel(bg);
        label.setSize(bg.getIconWidth(),bg.getIconHeight());
        UserIsUpdateInterface.super.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //2.把窗口面板设为内容面板并设为透明、流动布局。
        JPanel p1=(JPanel)UserIsUpdateInterface.super.getContentPane();
        p1.setOpaque(false);
        p1.setLayout((new FlowLayout()));

        Container container = this.getContentPane();

        //设置标签字体
        Font font = new Font("楷体", Font.BOLD, 20);

        //创建标签
        JLabel label1 = new JLabel("账号：");
        label1.setFont(font );
        label1.setForeground(Color.black);
        label1.setBounds(350,100,100,40);

        JLabel label2 = new JLabel("密码:");
        label2.setFont(font );
        label2.setForeground(Color.black);
        label2.setBounds(350,150,100,40);

        JLabel label3 = new JLabel("是否是管理:");
        label3.setFont(font );
        label3.setForeground(Color.black);
        label3.setBounds(350,200,100,40);

        JLabel label4 = new JLabel("昵称:");
        label4.setFont(font );
        label4.setForeground(Color.black);
        label4.setBounds(350,250,100,40);

        JLabel label5 = new JLabel("身份:");
        label5.setFont(font );
        label5.setForeground(Color.black);
        label5.setBounds(350,300,100,40);

        JLabel label6 = new JLabel("已借书籍:");
        label6.setFont(font );
        label6.setForeground(Color.black);
        label6.setBounds(350,350,100,40);

//        JLabel label7 = new JLabel("总数:");
//        label7.setFont(font );
//        label7.setBounds(150,400,100,40);
//
//        JLabel label8 = new JLabel("存放地点:");
//        label8.setFont(font );
//        label8.setBounds(150,450,100,40);

        container.add(label1);
        container.add(label2);
        container.add(label3);
        container.add(label4);
        container.add(label5);
        container.add(label6);
//        container.add(label7);
//        container.add(label8);

        //创建文本框
        JTextField textField1 = new JTextField();
        //将选中行现有的值放入文本框中 , 下同
        textField1.setText(rowdata[index][0].toString());
        textField1.setBounds(450,100,300,40);
        textField1.setFont(font);

        JTextField textField2 = new JTextField();
        textField2.setText(rowdata[index][1].toString());
        textField2.setBounds(450,150,300,40);
        textField2.setFont(font);

        JTextField textField3 = new JTextField();
        textField3.setText(rowdata[index][2].toString());
        textField3.setBounds(450,200,300,40);
        textField3.setFont(font);

        JTextField textField4 = new JTextField();
        textField4.setText(rowdata[index][3].toString());
        textField4.setBounds(450,250,300,40);
        textField4.setFont(font);

        JTextField textField5 = new JTextField();
        textField5.setText(rowdata[index][4].toString());
        textField5.setBounds(450,300,300,40);
        textField5.setFont(font);

        JTextField textField6 = new JTextField();
        textField6.setText(rowdata[index][5].toString());
        textField6.setBounds(450,350,300,40);
        textField6.setFont(font);

        container.add(textField1);
        container.add(textField2);
        container.add(textField3);
        container.add(textField4);
        container.add(textField5);
        container.add(textField6);

        //创建按钮
        JButton button = new JButton("保存");
        button.setBounds(500,400,80,50);
        button.setFont(font);

        //this.id = (Long) rowdata[index][0];
        UserService userService = new UserServiceImpl();
        //保存按钮监听
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean flag;

                String account = textField1.getText();
                String password = textField2.getText();
                Long isCaretaker = Long.valueOf(textField3.getText());
                String nickName = textField4.getText();
                Long categoryId = Long.valueOf(textField5.getText());
                Long alreadyNumber = Long.valueOf(textField6.getText());

                User newuser = new User(account,password,isCaretaker,nickName,categoryId,alreadyNumber);
                flag = userService.updateUser(newuser);
                if(flag){
                    new MyDialogDemo("修改成功!");
                } else {
                    new MyDialogDemo("修改失败");
                }
            }
        });
        //添加
        container.add(button);
        //获得容器
        container.setBackground(new Color(50, 144, 236));
        setBounds(380,200,1100,650);
        setLayout(null);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }
}
